package basic.JUC.print100;

import basic.JUC.FirstTrial;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/13 13:10
 * @描述 暂无
 */
public class ThreadPrint {
    public static void main(String[] args) throws InterruptedException {
//        printWithExtends();
//        threadTest();
        printStream();
        printY();
    }

    /**
     * 使用锁在循环中自然打印而不是判断后再打印
     */
    public static void printStream() throws InterruptedException {
        Object lock =new Object();
        Thread thread1=new Thread(()->{
            synchronized (lock){
                for(int i=1;i<100;i=i+2){
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    FirstTrial.printThreadName();
                    System.out.println(i);
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.notify();
                }
            }
        });
        Thread thread2=new Thread(()->{
            synchronized (lock){
                for(int i=2;i<=100;i=i+2){
                    lock.notify();
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    FirstTrial.printThreadName();
                    System.out.println(i);
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println("wdnmd");
    }

    public static void printWithExtends() throws InterruptedException {
        Object lock = new Object();
        class t1 implements Runnable {
            @Override
            public void run() {
                System.out.println("线程 " + Thread.currentThread().getName() + " 开始运行");
                for (int i = 0; i < 100; i++) {
                    synchronized (lock) {
                        if (i % 2 == 0) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + " :" + i);
                        } else {
                            try {
                                System.out.println(Thread.currentThread().getName() + "         wait一下");
                                lock.wait();
                                System.out.println("wait之后执行的线程:" + Thread.currentThread().getName());
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        lock.notify();
                    }
                }
            }
        }
        class t2 implements Runnable {
            @Override
            public void run() {
                System.out.println("线程 " + Thread.currentThread().getName() + " 开始运行");
                for (int i = 1; i < 100; i = i + 1) {
                    synchronized (lock) {
                        if (i % 2 == 1) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + " :" + i);
                        } else {
                            try {
                                System.out.println(Thread.currentThread().getName() + "         wait一下");
                                lock.wait();
                                System.out.println("wait之后执行的线程:" + Thread.currentThread().getName());
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        lock.notify();
                    }
                }
            }
        }
        Thread t11 = new Thread(new t1());
        t11.start();
        Thread.sleep(1000);
        Thread t22 = new Thread(new t2());
        t22.start();
    }


    public static void printY(){
        final Object object = new Object();
        char[] a1 = "1234567".toCharArray();
        char[] a2 = "ABCDEFG".toCharArray();

        new Thread(() -> {
            synchronized (object){
                for(char c : a1){
                    System.out.print(c);
                    try{
                        object.notify();
                        object.wait();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                object.notify();//避免有线程未被唤醒
            }
        },"t1").start();

        new Thread(() -> {
            synchronized (object){
                for(char c : a2){
                    System.out.print(c);
                    try{
                        object.notify();
                        object.wait();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                object.notify();//避免有线程未被唤醒
            }
        },"t2").start();
    }

    public static void threadTest() throws InterruptedException {
        Object lock = new Object();
        Thread thread1 = new Thread(() -> {
            for (int i = 1; i <= 100; i++) {
                synchronized (lock) {
                    if (i % 2 == 1) {
                        System.out.println(Thread.currentThread().getName() + "  " + i);
                    } else {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    lock.notify();
                }
            }
        });
        Thread thread2 = new Thread(() -> {

            for (int i = 2; i <= 100; i++) {
                synchronized (lock) {
                    if (i % 2 == 0) {
                        System.out.println(Thread.currentThread().getName() + "  " + i);
                    } else {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    lock.notify();
                }
            }
        });
        thread1.start();
        Thread.sleep(1000);
        thread2.start();

    }

}
